let timer1, timer2

/**
 * 滚动条横向和纵向动画复位
 */
export default function resetScroll() {
    clearInterval(timer1)
    clearInterval(timer2)
    const html = document.documentElement
    const body = document.body
    const scrollTop = html.scrollTop || body.scrollTop
    const scrollLeft = html.scrollLeft || body.scrollLeft
    timer1 = changeNumToNum(scrollTop, 0, 500, (num) => {
        html.scrollTop = body.scrollTop = num
    })
    timer2 = changeNumToNum(scrollLeft, 0, 500, (num) => {
        html.scrollLeft = body.scrollLeft = num
    })
}

/**
 * 在n秒内，从指定的初始值缓慢变化到结束值
 * @param {number} startNum 初始值
 * @param {number} endNum 结束值
 * @param {number} duration 缓慢变化的时间，单位毫秒
 */
function changeNumToNum(startNum, endNum, duration = 500, cb) {
    const tick = 16 // 每一帧的时间，单位毫秒，每隔16毫秒变化一次
    const times = Math.ceil(duration / tick)  // 变化的次数
    let currentTimes = 0 // 当前变化的次数
    const step = (endNum - startNum) / times // 每次变化的步长
    const timer = setInterval(() => {
        currentTimes++
        startNum += step
        if (currentTimes > times) {
            startNum = endNum
            clearInterval(timer)
            return
        }
        cb(startNum)
    }, duration)
    return timer
}